﻿using LX.Core.Validation;
using S4Store.Domain;
using S4Store.Persistent;
using System.Linq;

namespace S4Store.Service.Valid.AdministratorValid
{
    public class Add : ValidExpression<Administrator>
    {
        public S4StoreDataContext PersistentContext { get; set; }

        public override void ValidEntity(ValidationResult result, Administrator entity)
        {
            NoEmpty(result, "名称", entity.Name);
            NoEmpty(result, "用户名", entity.UserName);
            NoEmpty(result, "密码", entity.Password);
            if(entity.Position!= Position.GeneralManager&&entity.StoreId==null)
            {
                PushError(result, "请选择所属店");
            }
            if (PersistentContext.Set<Administrator>().AsQueryable().FirstOrDefault(t => t.UserName == entity.UserName) != null)
            {
                PushError(result, "已存在相同的用户名");
            }
        }
    }
}
